<?php

namespace App\EventListener;

use App\Entity\AdminUser;
use App\Utils\Helper\GeneralHelper;
use App\Utils\Helper\GetterHelper;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;

class AuthenticationSuccessListener {
    /**
     * @param AuthenticationSuccessEvent $event
     * @throws \Exception
     */
    public function onAuthenticationSuccessResponse (AuthenticationSuccessEvent $event) {
        $data = [
            'result' => [
                'success' => true,
                'code'    => 200,
                'message' => '操作成功',
                'version' => GetterHelper::getParameter('frontend.version'),
            ],
        ];
        $content = $event->getData();
        $user = $event->getUser();

        if (!$user instanceof AdminUser) {
            return;
        }

        $content['profile'] = [
            'id'       => $user->getId(),
            'username' => $user->getUsername(),
            'nickname' => $user->getNickname(),
            'contact'  => $user->getContact(),
            'email'    => $user->getEmail(),
            'avatar'   => GeneralHelper::getAvatar($user->getAvatar()),
            'motto'    => $user->getMotto(),
        ];
        $data['content'] = $content;

        $event->setData($data);
    }
}